import { keepIntactOrderType } from '@/store/type/index'
import { clone } from 'lodash'

const keepIntactOrderReducer = (
  state = {
    // 初始化数据
    tableList: [],
    tabActive: '',
    tabList: [],
    allList: [],
  },
  action
) => {
  let stateCopy = clone(state)
  switch (action.type) {
    case keepIntactOrderType.setAllList:
      stateCopy.allList = action.payload
    case keepIntactOrderType.setList:
      stateCopy.tableList = stateCopy.allList.filter(
        (item) => item.policyType === stateCopy.tabActive
      )
      break
    case keepIntactOrderType.setTabs:
      stateCopy.tabList = action.payload
      break
    case keepIntactOrderType.setTabActive:
      stateCopy.tabActive = action.payload
      break
    case keepIntactOrderType.changeRowApplicationDate:
      const date = new Date().toLocaleString()
      stateCopy.tableList = stateCopy.tableList.map((element) => {
        if (element.preserveId === action.payload) {
          element.applicationDate = date
        }
        return element
      })
      stateCopy.allList = stateCopy.allList.map((element) => {
        if (element.preserveId === action.payload) {
          element.applicationDate = date
        }
        return element
      })
      break
    default:
      break
  }
  return stateCopy
}
export default keepIntactOrderReducer
